home *** CD-ROM | disk | FTP | other *** search
/ 220 Jogos / 220 jogos.iso / tetris / tetron / SOURCE / WINMAIN.CPP < prev   
Encoding:
C/C++ Source or Header  |  2002-08-14  |  5.3 KB  |  191 lines

  1. //----------------------------------------------------------------------------------------
  2. //----------------------------------------------------------------------------------------
  3. //
  4. //        Filename        :    winmain.cpp
  5. //        Description        :    Main Win32 Functions
  6. //        Author            :   Marnich van Rensburg (2002)
  7. //
  8. //----------------------------------------------------------------------------------------
  9. //----------------------------------------------------------------------------------------
  10.  
  11. // Note: Linker requires (winmm.lib opengl32.lib glu32.lib glaux.lib) 
  12.  
  13. #include "gamemain.h"
  14.  
  15. //----------------------------------------------------------------------------------------
  16. //        Globals
  17. //----------------------------------------------------------------------------------------
  18.  
  19. bool StarFieldActive = true;
  20. bool SoundActive = true;
  21.  
  22. //----------------------------------------------------------------------------------------
  23. //        Variable/Function Declarations
  24. //----------------------------------------------------------------------------------------
  25.  
  26. bool   keys[256];        // Array Used For The Keyboard Routine
  27. bool   active = true;    // Window Active Flag Set To true By Default
  28. unsigned char    KeyVal;
  29. unsigned long keyhold = 0;    //Time keydown event
  30.  
  31. GameMain CurrentGame;
  32.  
  33. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  34.  
  35. //----------------------------------------------------------------------------------------
  36. //        Winmain Functions    :    WndProc
  37. //----------------------------------------------------------------------------------------
  38.  
  39. LRESULT CALLBACK WndProc(HWND   hWnd,   // Handle For This Window
  40.                          UINT   uMsg,   // Message For This Window
  41.                          WPARAM wParam, // Additional Message Information
  42.                          LPARAM lParam) // Additional Message Information
  43. {
  44.   
  45.     switch (uMsg)                        // Check For Windows Messages
  46.     {      
  47.         case WM_ACTIVATE:                // Watch For Window Activate Message
  48.         {
  49.             if (!HIWORD(wParam))        // Check Minimization State
  50.             {
  51.                 active = true;            // Program Is Active
  52.             }
  53.             else
  54.             {
  55.                 active = false;            // Program Is No Longer Active
  56.             }//if - else
  57.  
  58.             return 0;                    // Return To The Message Loop
  59.         }//case
  60.  
  61.       
  62.         case WM_SYSCOMMAND:                // Intercept System Commands
  63.         {
  64.             switch (wParam) {            // Check System Calls
  65.                 case SC_SCREENSAVE:        // Screensaver Trying To Start?
  66.                 case SC_MONITORPOWER:    // Monitor Trying To Enter Powersave?
  67.                 return 0;                // Prevent From Happening
  68.             }//switch
  69.  
  70.             break;                        // Exit
  71.         }//case
  72.  
  73.       
  74.         case WM_CLOSE:                    // Did We Receive A Close Message?
  75.         {
  76.             PostQuitMessage(0);            // Send A Quit Message
  77.             return 0;                    // Jump Back
  78.         }//case
  79.  
  80.  
  81.         case WM_KEYDOWN:                // Is A Key Being Held Down?
  82.         {
  83.             KeyVal = wParam;
  84.             if(keyhold == 0)
  85.             {
  86.                 keys[wParam] = true;        // If So, Mark It As true
  87.                 keyhold = 1;
  88.             }else{
  89.                 keys[wParam] = false;        // If So, Mark It As true
  90.                 keyhold++;
  91.             }//if
  92.             return 0;                    // Jump Back
  93.         }
  94.  
  95.       
  96.         case WM_KEYUP:                    // Has A Key Been Released?
  97.         {
  98.             keyhold = 0;
  99.             keys[wParam] = false;        // If So, Mark It As false
  100.             return 0;                    // Jump Back
  101.         }//case
  102.  
  103.  
  104.         case WM_SIZE:                    // Resize The OpenGL Window
  105.         {
  106.             return 0;                    // Jump Back
  107.         }//case
  108.     
  109.     }//switch
  110.  
  111.     return DefWindowProc(hWnd, uMsg, wParam, lParam);    // Pass All Unhandled Messages To DefWindowProc
  112.  
  113. }//WndProc
  114.  
  115.  
  116.  
  117.  
  118. //----------------------------------------------------------------------------------------
  119. //        Winmain Functions    :    WinMain
  120. //----------------------------------------------------------------------------------------
  121.  
  122. int WINAPI WinMain(HINSTANCE hInstance,        // Instance
  123.                    HINSTANCE hPrevInstance, // Previous Instance
  124.                    LPSTR    lpCmdLine,        // Command Line Parameters
  125.                    int        nCmdShow)        // Window Show State
  126. {
  127.     MSG msg;
  128.     bool   done = false;                        // Bool Variable To Exit Loop
  129.  
  130.     if (CurrentGame.Init())                        // Initialize the game
  131.     {
  132.         CurrentGame.New();                        // Starts a new game
  133.  
  134.         while(!done)                            // Loop While done = false   
  135.         {        
  136.             if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))    // Is There A Message Waiting?
  137.             {
  138.                 if (msg.message == WM_QUIT)        // Have We Received A Quit Message?
  139.                 {
  140.                     done = true;                // If So done=true
  141.                 }
  142.                 else                            // If Not, Deal With Window Messages
  143.                 {
  144.                     TranslateMessage(&msg);        // Translate The Message
  145.                     DispatchMessage(&msg);        // Dispatch The Message
  146.                 }//if - else
  147.             }
  148.             else
  149.             {
  150.                 if (keys[VK_ESCAPE])            // Check if there was a message
  151.                 { 
  152.                     done = true;   
  153.                 }
  154.                 else                            // if there wasn't a message
  155.                 { 
  156.                     
  157.                     switch(CurrentGame.Mode)
  158.                     {
  159.                         case GAME_START_SCREEN:
  160.                         {
  161.                             CurrentGame.New();                        // Starts a new game
  162.                             CurrentGame.StartScreen(KeyVal);
  163.                             break;
  164.                         }//case
  165.  
  166.                         case GAME_ACTIVE:
  167.                         {
  168.                             CurrentGame.Play(KeyVal);            // Enter Gameloop
  169.                             break;
  170.                         }//case
  171.  
  172.                         case GAME_NEW_HIGHSCORE:
  173.                         {
  174.                             CurrentGame.GetHighScore(KeyVal);            // Enter Gameloop
  175.                             break;
  176.                         }//case
  177.  
  178.                     }//switch
  179.  
  180.                     KeyVal = 0;
  181.                 }//if - else 
  182.             }//if - else
  183.         }//while
  184.  
  185.         CurrentGame.Quit();
  186.  
  187.     }//if
  188.  
  189.     return(msg.wParam);                            // Return from the program
  190.  
  191. }//WinMain